package com.zd.commodity.productMng.action;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.alibaba.fastjson.JSON;
import com.opensymphony.xwork2.ActionContext;
import com.zd.commodity.action.BaseAction;
import com.zd.commodity.productMng.biz.IProductAttrDictBiz;
import com.zd.commodity.productMng.entity.ProductAttrDict;
import com.zd.commodity.userMng.entity.User;
import com.zd.commodity.dto.ProductAttrDictExcel;
import com.zd.commodity.dto.Solr;

@Scope("prototype")
@Controller("productAttrDictAction")
public class ProductAttrDictAction extends BaseAction<ProductAttrDict> {

	private IProductAttrDictBiz productAttrDictBiz;
	private List<ProductAttrDict> attrDicList;

	public IProductAttrDictBiz getProductAttrDictBiz() {
		return productAttrDictBiz;
	}

	@Autowired
	public void setProductAttrDictBiz(IProductAttrDictBiz productAttrDictBiz) {
		this.productAttrDictBiz = productAttrDictBiz;
	    this.setBaseBiz(productAttrDictBiz);
	}
	private String categoryName;
	private String attrGroupName;
	private String attrName;
	private String isKeyAttr;
	//根据属性id导出属性数据字典
	public void exportDictByAttr() throws Exception{
		Map<String, String> map = new HashMap<String, String>();
		ProductAttrDict dict=super.getT1();
		map.put("attrId", String.valueOf(dict.getProductAttr().getId()));
		map.put("categoryName", categoryName);
		map.put("attrGroupName", attrGroupName);
		map.put("attrName", attrName);
		map.put("isKeyAttr", isKeyAttr.equals("true")?"是":"否");
		
		List list = productAttrDictBiz.getExcelExportListBySql(ProductAttrDictExcel.class,"exportDictByAttr",map.size()>0?map:null, -1, -1);
		
		String[] title = new String[]{"4级分类名称","属性组名称","属性名称","是否为关键属性","原始值","标准值","简称"};
		String[] neededFields = new String[]{"categoryName","attrGroupName","attrName","isKeyAttr","originalValue","standardValue","shortValue"};
		super.excelExport("商品属性管理", list,title,neededFields);
	}
	
	public void addAttrDict(){
		ProductAttrDict attrDict = super.getT();
		User user = (User) super.request.getSession().getAttribute("user");
		attrDict.setCreator(user.getUsername());
		attrDict.setCreationTime(new Timestamp(System.currentTimeMillis()));
		try {
			productAttrDictBiz.add(attrDict);
			write(ajaxReturn(true,"添加成功！",null));
		} catch (Exception e) {
			write(ajaxReturn(false,"添加失败！",null));
			e.printStackTrace();
		}
	}

	public String listByPageBySql()
	{	
		ProductAttrDict t1=super.getT1();
		String params=super.getParams();
		int page=super.getPage();
		int rows=super.getRows();
		
		Map<String, String> map = new HashMap<String, String>();
		if(t1!=null){
			map.put("attrName", t1.getProductAttr().getName().toString());
		}else{
			map.put("attrName", "");
		}
		
		//将参数转型
		Object objparam=(Object)params;
		//页码换算
		if(page==0)
		{
			page=1;
		}
		if(rows==0)
		{
			rows=10;
		}	
		
		int firstResult=(page-1)*rows;
		//获取列表
		List<Solr> list = productAttrDictBiz.getPagedListBySql(Solr.class, "solr", map, firstResult, rows);
		//统计记录个数
		long count = productAttrDictBiz.getPagedListCountBySql("solrCount", map);
		long pageCount=count/rows;
		if(count%rows>0){
			pageCount++;
		}
		ActionContext.getContext().put("list", list);
		ActionContext.getContext().put("pageCount",pageCount);
		ActionContext.getContext().put("page",page);
		ActionContext.getContext().put("recordsCount",count);
		
		
		return "list";
	}
	//根据属性id查询数据字典列表，返回ajax数据
	public void dicList(){
		ProductAttrDict t1=super.getT1();
		//获取列表
		List<ProductAttrDict> list = productAttrDictBiz.getList(t1,null,null);
		write(JSON.toJSONString(list));
	}
	//根据id修改数据字典列表，返回ajax数据
	public void updateAttrDict(){
		ProductAttrDict t=super.getT();
		User user = (User) super.request.getSession().getAttribute("user");
		t.setUpdater(user.getUsername());
		t.setUpdateTime(new Timestamp(System.currentTimeMillis()));
		try {
			productAttrDictBiz.update(t);
			write(ajaxReturn(true,"修改成功！",null));
		} catch (Exception e) {
			write(ajaxReturn(false,"修改失败！",null));
			e.printStackTrace();
		}
	}
	
	public String list()
	{
		ProductAttrDict t1=super.getT1();
		ProductAttrDict t2=super.getT2();
		//获取列表
		List<ProductAttrDict> list = productAttrDictBiz.getList(t1,t2,null);				
		ActionContext.getContext().put("list", list);
		ActionContext.getContext().put("page", super.getPage());
		ActionContext.getContext().put("method", "update");
		return "form";
		
	}

	public List<ProductAttrDict> getAttrDicList() {
		return attrDicList;
	}

	public void setAttrDicList(List<ProductAttrDict> attrDicList) {
		this.attrDicList = attrDicList;
	}

	public String getCategoryName() {
		return categoryName;
	}

	public void setCategoryName(String categoryName) {
		this.categoryName = categoryName;
	}

	public String getAttrGroupName() {
		return attrGroupName;
	}

	public void setAttrGroupName(String attrGroupName) {
		this.attrGroupName = attrGroupName;
	}

	public String getAttrName() {
		return attrName;
	}

	public void setAttrName(String attrName) {
		this.attrName = attrName;
	}

	public String getIsKeyAttr() {
		return isKeyAttr;
	}

	public void setIsKeyAttr(String isKeyAttr) {
		this.isKeyAttr = isKeyAttr;
	}
}
